﻿using K9Nano.Application.Dtos;
using K9Nano.Application.Organization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace K9Nano.AspNetCore.Controllers;

[Route("admin/api/org")]
public class OrganizationUnitController(IOrganizationUnitAppService service) : ApiControllerBase
{
    [HttpPost]
    public async Task<OrganizationUnitDto> Create(OrganizationUnitCreateDto input)
    {
        return await service.CreateAsync(input, HttpContext.RequestAborted);
    }

    [HttpPut]
    public async Task<OrganizationUnitDto> Update(OrganizationUnitEditDto input)
    {
        return await service.UpdateAsync(input, HttpContext.RequestAborted);
    }

    [HttpDelete("{id}")]
    public async Task Delete(long id)
    {
        await service.DeleteAsync(id, HttpContext.RequestAborted);
    }

    [HttpGet("children")]
    public async Task<IReadOnlyList<OrganizationUnitDto>> GetChildren(long? parentId, bool recursive = false)
    {
        return await service.GetChildrenAsync(parentId, recursive, HttpContext.RequestAborted);
    }

    [HttpGet("{id}")]
    public async Task<OrganizationUnitDto?> Get(long id)
    {
        return await service.GetAsync(id, HttpContext.RequestAborted);
    }

    [HttpGet("query")]
    public virtual async Task<PagedResultDto<OrganizationUnitDto>> Query([FromQuery]OrganizationUnitQueryDto dto)
    {
        return await service.QueryAsync(dto, HttpContext.RequestAborted);
    }
}
